package com.smedi.ismedi.notification.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.notification.api.dto.request.MailTemplateRequest;
import com.smedi.ismedi.notification.api.dto.response.MailTemplateResponse;
import com.smedi.ismedi.notification.api.feign.RemoteMailTemplateFeignApi;
import com.smedi.ismedi.notification.core.application.command.MailTemplateCommand;
import com.smedi.ismedi.notification.core.application.query.MailTemplateQueryService;
import com.smedi.ismedi.notification.core.application.query.vo.MailTemplateVo;
import com.smedi.ismedi.notification.core.application.service.MailTemplateApplicationService;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.MailTemplate;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: MailTemplateController
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/11/23 10:34
 */

@Slf4j
@RestController
@AllArgsConstructor
public class MailTemplateController implements RemoteMailTemplateFeignApi {

    private final DozerUtil dozerUtil;

    private final MailTemplateApplicationService mailTemplateApplicationService;

    private final MailTemplateQueryService mailTemplateQueryService;

    @Override
    public ResponseData<MailTemplateResponse> postSaveMailTemplate(MailTemplateRequest mailTemplateRequest) {
        MailTemplateCommand mailTemplateCommand = dozerUtil.convertor(mailTemplateRequest, MailTemplateCommand.class);
        MailTemplate mailTemplate = mailTemplateApplicationService.saveTemplate(mailTemplateCommand);
        return new ResponseData<MailTemplateResponse>().success(dozerUtil.convertor(mailTemplate, MailTemplateResponse.class));
    }

    @Override
    public ResponseData<Boolean> deleteMailTemplate(Long mailTemplateId) {
        mailTemplateApplicationService.deleteTemplate(mailTemplateId);
        return new ResponseData<Boolean>().success(true);
    }

    @Override
    public ResponseData<PaginationData<MailTemplateResponse>> getQueryTemplateTable(Long offset, Long pageSize, String templateName) {
        IPage pagination = mailTemplateQueryService.getMailTemplatePaginationByTemplateName(offset, pageSize, templateName);
        pagination.setRecords(dozerUtil.convertor(pagination.getRecords(), MailTemplateResponse.class));

        PaginationData PaginationData = dozerUtil.convertor(pagination, PaginationData.class);
        return new ResponseData<PaginationData<MailTemplateResponse>>().success(PaginationData);
    }

    @Override
    public ResponseData<MailTemplateResponse> getQueryMailTemplate(Long mailTemplateId) {
        MailTemplateVo mailTemplateVo = mailTemplateQueryService.getMailTemplateByMailTemplateId(mailTemplateId);
        return new ResponseData<MailTemplateResponse>().success(dozerUtil.convertor(mailTemplateVo, MailTemplateResponse.class));
    }
}